<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
class AddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询数据库。显示收货地址
        $id=session('homeuser')->id;
         $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
         $type=DB::table('type')->get()->all();
        $result= DB::table('receive_address')->where('user_id',$id)->first();
        $links=DB::table('links')->get();
       
        if(!empty($result)){

            $address = DB::select( 'select * from `user` inner join `receive_address` on `user`.`id` = `receive_address`.`user_id` where `user`.`id` = '.$id);

           return view('home.address',['address'=>$address,'links'=>$links,'search'=>$ss,
         'all'=>$type]);
        }else{
           return view('home.address',['links'=>$links,'search'=>$ss,
         'all'=>$type]);
        }  
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // return view('home.addaddress');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      //插入数据
      $data = $request->except('_token');
      $links=DB::table('links')->get();

      $province = $request->only('province');
        $pro = DB::table('district')->where('id',$province['province'])->get();
      
        $data = $request->except('_token','product_id','province');
        $data['province'] = $pro[0]->name;
        //dd($data);
        $m = DB::table('receive_address')->insertGetId($data);
        if($m>0){
            return redirect('home/address');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //显示收货地址修改界面
         $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
         $type=DB::table('type')->get()->all();
        $links=DB::table('links')->get();
        $address = DB::table('receive_address')->where('id',$id)->first();
       //dd($address);
        return view('home.editaddress',['address'=>$address,'links'=>$links,'search'=>$ss,
         'all'=>$type]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //修改数据更新数据库
        $links=DB::table('links')->get();
        $data = $request->except('_token','_method','province');
        $province = $request->only('province');
        $pro = DB::table('district')->where('id',$province['province'])->get();
        if(empty($pro[0]->name)){
          $data = $request->except('_token','_method','province');
        }else{
        $data['province'] = $pro[0]->name;
         }
        $m = DB::table('receive_address')->where('id',$id)->update($data);
        if($m>0){
    		return redirect('home/address');
    	}else{
            return back()->with('msg','信息无更改');
       }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $links=DB::table('links')->get();
        $row = DB::table('receive_address')->where('id',$id)->delete();
        if($row>0){
            return redirect('home/address');
        }else{
            return redirect('home/address');
        }
    }
}
